#include "textclassify.h"
#include <QFileDialog>
#include <vector>
#include <string>
#include <QDirIterator>
#include "training.h"
#include <map>
#include <QMessageBox>
using namespace std;

TextClassify::TextClassify(QWidget *parent)
	: QWidget(parent),_classfier(NULL)
{
	ui.setupUi(this);
	_classfier = new Classifier;
	connect(ui.chooseFolderBtn, SIGNAL(clicked()), this, SLOT(chooseFolder()));
	connect(ui.chooseFileBtn, SIGNAL(clicked()), this, SLOT(chooseFile()));
	connect(ui.runBtn, SIGNAL(clicked()), this, SLOT(runAnalyse()));
	connect(&_progressThread,SIGNAL(notify(int)),this,SLOT(onNotify(int)));

	ui.progressBar->setVisible(false);
	setWindowTitle(tr("毕业设计"));
	setFixedSize(577,467);
}

TextClassify::~TextClassify()
{
	delete _classfier;
}

void TextClassify::onNotify(int i)
{  
	ui.progressBar->setValue(i);
}; 

// this function stores the absolute paths of each file in a QVector
std::vector<std::string> findFilesRecursively(QDir rootDir) {

	std::vector<std::string> filesVector;
	QDirIterator it(rootDir, QDirIterator::Subdirectories);
	while(it.hasNext()) {
		filesVector.push_back(it.next().toStdString());
	}
	//TraningManager::setTrainingFileList(filesVector);
	return filesVector;
}


void TextClassify::chooseFolder()
{
	QString dirpath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"D:/",
	QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
	ui.dataFolderLineEdit->setText(dirpath);
	_classfier->setRootPath(dirpath.toStdString());

	QDir selectedDir(dirpath);
	
	selectedDir.setFilter(QDir::Files |
		QDir::Dirs);
	
	QFileInfoList list = selectedDir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
	//vector<string> folderList;
	QStringList qsl; 
	qsl.append("*.pdf"); // I only want pdf files
	map< string, vector<string> > allFileList;

	foreach(QFileInfo finfo, list) {
		if (finfo.isDir())
		{
			QDir tempDir(finfo.filePath());

			tempDir.setNameFilters(qsl);
			vector<string> fileList = findFilesRecursively(tempDir);
			allFileList[finfo.fileName().toStdString()] = fileList;
			//folderList.push_back(finfo.fileName().toStdString());
		}
	}
	TraningManager::setAllFiles(allFileList);
	//TraningManager::setTrainingFolderList(folderList);
}

void TextClassify::chooseFile()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"D:/", tr("Pdf Files (*.pdf)"));
	ui.testDataFileLineEdit->setText(fileName);
	_classfier->setFilePath(fileName.toStdString());
}

void TextClassify::runAnalyse()
{
	if(_classfier->getFilePath().empty() && _classfier->getRootPath().empty())
	{
		QMessageBox::warning(this, "Please input data", "You must choose the path and folder!");
		return;
	}

	ui.progressBar->setVisible(true);
	_progressThread.test();
	_classfier->classify();
	string key = _classfier->getKeyList();
	ui.keyTextBrower->setText(QString(key.c_str()));
	string type = _classfier->getResult();
	_progressThread.terminate();
	ui.progressBar->setVisible(false);
	if (!type.empty())
	{
		QString result = QString("The file type is %1").arg(type.c_str());
		QMessageBox::information(this, "Result", result);
	}

}
